package com.claude.sql;

import java.io.File;

import com.claude.util.Configure;

import android.content.Context;
import android.content.ContextWrapper;
import android.database.DatabaseErrorHandler;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class SQLManager extends SQLiteOpenHelper {
	private String createAccount = "create table account(ano char(11) primary key, aname varchar(10), apassword varchar(50))";
	private String createSubject = "create table subject(suno char(11) primary key, suname varchar(10), ano char(11))";
	private String createSession = "create table session(seno char(11), sename varchar(15), suno char(11), leno char(10))";
	private String createStudent = "create table student(stno char(11), stname varchar(10), stcard char(10), seno char(11), sex char(4))";
	private String createCheck = "create table checkstu(chno varchar(50), stno char(10), chtype varchar(20), chtime char(20), chdate char(20), stcard char(10))";
	private String createTmpCheck = "create table tmpcheck(chno varchar(50), stno char(10), chtype varchar(20), chtime char(20),  chdate char(20), stcard char(10))";
	private String createTmpChecktea = "create table tmpchecktea(chno varchar(50), aname varchar(10), extime char(20), longitude varchar(25), latitude varchar(25), chtag char(1))";
	private String createChecktea = "create table checktea(chno varchar(50), aname varchar(10), extime char(20), longitude varchar(25), latitude varchar(25), chtag char(1))";
	private String createLession = "create table lession(leno char(10), letime char(20), leweek char(2))";

	final private static int VERSION = 2;
	final private static String name = "nightmare.db";

	public SQLManager(Context context, String name, int version,
			DatabaseErrorHandler databaseErrorHandler) {
		super(new MyContextWrapper(context), name, null, version,
				databaseErrorHandler);
	}

	public SQLManager(Context context) {
		super(new MyContextWrapper(context), name, null, VERSION);
	}

	public void onCreate(SQLiteDatabase db) {
		db.beginTransaction();
		try {
			db.execSQL(createAccount);
			db.execSQL(createSubject);
			db.execSQL(createSession);
			db.execSQL(createStudent);
			db.execSQL(createLession);
			db.execSQL(createCheck);
			db.execSQL(createTmpCheck);
			db.execSQL(createTmpChecktea);
			db.execSQL(createChecktea);
			db.setTransactionSuccessful();
		} catch (SQLiteException error) {

		} finally {
			db.endTransaction();
		}
	}

	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.beginTransaction();
		try {
			db.execSQL(createCheck);
			db.execSQL(createTmpCheck);
			db.execSQL(createTmpChecktea);
			db.execSQL(createChecktea);
			db.setTransactionSuccessful();
		} catch (SQLException error) {

		} finally {
			db.endTransaction();
		}
	}
}

class MyContextWrapper extends ContextWrapper {

	private String mPath = Configure.getInstance().getPath();

	public MyContextWrapper(Context base, String path) {
		super(base);
		mPath = path;
	}

	public MyContextWrapper(Context base) {
		super(base);
	}

	public File getDatabasePath(String name) {
		File result = new File(mPath + File.separator + name);
		if (!result.getParentFile().exists()) {
			result.getParentFile().mkdirs();
		}
		return result;
	}

	public SQLiteDatabase openOrCreateDatabase(String name, int mode,
			CursorFactory factory) {
		return SQLiteDatabase.openOrCreateDatabase(getDatabasePath(name),
				factory);
	}

	public SQLiteDatabase openOrCreateDatabase(String name, int mode,
			CursorFactory factory, DatabaseErrorHandler errorHandler) {
		return SQLiteDatabase.openOrCreateDatabase(getDatabasePath(name)
				.getAbsolutePath(), factory, errorHandler);
	}
}
